<?php

	class suppliers {
		

		function suppliers() {
			global $admin;

			//if(empty($_SESSION["managerid"])) $admin->login();
		}


		function index() {
			global $admin;
			//if(!$admin->rights("suppliers_view")) return;
		}


		function create() {
			global $db, $smarty, $config, $admin;
			//if(!$admin->rights("suppliers_create")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="create") {
				if(empty($_POST["title"])) $errors["empty_title"] = true;
				if(empty($_POST["classid"])) $errors["empty_classtitle"] = true;
				if(empty($errors) && $db->getone("select count(1) from {$config->db_prefix}suppliers where title=".$db->qstr($_POST["title"]))) $errors["exist_title"] = true;

				if(empty($errors)) {
					$title = $db->qstr($_POST["title"]);
					$linkman = $db->qstr($_POST["linkman"]);
					$phone = $db->qstr($_POST["phone"]);
					$address = $db->qstr($_POST["address"]);
					$memo = $db->qstr($_POST["memo"]);
					$classid = $db->qstr($_POST["classid"]);
					$shopid = $db->qstr($_POST["shopid"]);
					$is_general = $db->qstr($_POST["is_general"]);
					$sqlstr = "insert into {$config->db_prefix}suppliers (title, linkman, phone, address, memo,classid,shopid,is_general) values ($title, $linkman, $phone, $address, $memo, $classid,$shopid,$is_general)";
					$result = $db->execute($sqlstr);
					if($result) header("location:{$config->site_admin}m=inventory&s=suppliers.php");
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
		}


		function modify() {
			global $db, $smarty, $config, $admin;
			//if(!$admin->rights("suppliers_modify")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="modify") {
				$id = $db->qstr($_POST["id"]);
				
				if(empty($_POST["title"])) $errors["empty_title"] = true;
				if(empty($_POST["classid"])) $errors["empty_classtitle"] = true;
				if(empty($errors) && $db->getone("select count(1) from {$config->db_prefix}suppliers where id!=$id and title=".$db->qstr($_POST["title"]))) $errors["exist_title"] = true;

				if(empty($errors)) {
					$title = $db->qstr($_POST["title"]);
					$linkman = $db->qstr($_POST["linkman"]);
					$phone = $db->qstr($_POST["phone"]);
					$address = $db->qstr($_POST["address"]);
					$memo = $db->qstr($_POST["memo"]);
					$classid = $db->qstr($_POST["classid"]);
					$shopid = $db->qstr($_POST["shopid"]);
					$is_general = $db->qstr($_POST["is_general"]);
					
					//查看是否改变了仓库
					$sql = "select shopid from {$config->db_prefix}suppliers where id={$id}";
					$oldShopId = $db->getone($sql);
					
					$sqlstr = "update {$config->db_prefix}suppliers set title=$title, linkman=$linkman, phone=$phone, address=$address, memo=$memo, classid=$classid,shopid=$shopid,is_general={$is_general} where id=$id";
					$result = $db->execute($sqlstr);
					
					
					
					if ($result) {
					    //改变了仓库则更新供应商产品表
					    if ($oldShopId != $shopid) {
					        $sql = "update {$config->db_prefix}supplie_products set shopid={$shopid} where supplieid={$id}";
					        $db->execute($sql);
					    }
					    
					    header("location:".(empty($_POST["referer"])?"{$config->site_admin}m=inventory&s=suppliers.php":$_POST["referer"]));
					}
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);
			}
		}


		function remove($id) {
			global $db, $segment, $config, $admin;
			//if(!$admin->rights("suppliers_delete")) return;

			$supplierid = empty($id)?$db->qstr($_GET['id']):$id;
			$sqlstr = "delete from {$config->db_prefix}suppliers where id=$supplierid";
			$result = $db->execute($sqlstr);
			if($result && empty($id)) header("location:".(empty($_SERVER['HTTP_REFERER'])?"{$config->site_admin}m=inventory&s=suppliers.php":$_SERVER['HTTP_REFERER']));
		}
		
}
?>